<#include "../base/base.ftl">
<@body>
<div class="wu-toolbar-search" id="tb">
    <a href="#" id="refresh" class="easyui-linkbutton" data-options="iconCls:'icon fa fa-refresh',plain:true">刷新</a>
    <a href="#" id="add" class="easyui-linkbutton" data-options="iconCls:'icon fa fa-plus',plain:true">新增</a>
    <a href="#" id="edit" class="easyui-linkbutton" data-options="iconCls:'icon fa fa-pencil-square-o',plain:true">编辑</a>
    <a href="#" id="del" class="easyui-linkbutton" data-options="iconCls:'icon fa fa-minus-circle',plain:true">删除</a>
    <br>
    <label>字典名称：</label><input class="wu-text" id="name" style="width:100px">
    <label>字典代码：</label><input class="wu-text" id="code" style="width:100px">
    <#--<label>起始时间：</label><input id="startTime" class="easyui-datebox" style="width:100px" data-options="editable:false">-->
    <#--<label>结束时间：</label><input id="endTime" class="easyui-datebox" style="width:100px" data-options="editable:false">-->
    <a class="easyui-linkbutton" iconCls="icon-search" onclick="find()">条件搜索</a>
</div>
<table id="tt" >
</table>
</@body>
<@js>
<script type="text/javascript">
$(function(){
    $('#tt').datagrid({
        singleSelect:true,
        idField:'id',
        rownumbers:true,
        toolbar: '#tb',
        pageSize:20,
        pagination:true,
        fitColumns:true,
        fit:true,
        method:'get',
        url:'/dictionary/getData',
        onDblClickRow: function(index,field,value){
            $("#edit").click();
        },
        columns:[[
            {field:'name',title:'字典名称',width:100,sortable:false,align: 'center'},
            {field:'code',title:'字典代码',width:100,sortable:false,align: 'center'},
            {field:'remark',title:'备注',width:100,sortable:false,align: 'center'},
//            {field:'createTime',title:'创建时间',width:80,align:'center'}
        ]]
    });

    $("#startTime").datebox("setValue", myformatter(getCurrentMonthFirst()));
    var curr_time = new Date();
    $("#endTime").datebox("setValue", myformatter(curr_time));

    $("#refresh").click(function () {
        $("#tt").datagrid('reload',{});
    });

    $("#add").click(function () {
        $("<div></div>").dialog({
            id:'addView',
            title:'新增',
            resizable: true,
            closable: true,
            width: 600,
            height: 500,
            modal:true,
            href: "/dictionary/view",
            onClose:function () {
                $("#addView").dialog('destroy');
                $("#tt").datagrid('reload');
            },
            onDestroy: function () {
                $("#tt").datagrid('reload');
            },
            buttons:[{
                iconCls: 'icon fa fa-check',
                text: '保存',
                handler: function () {
                    var index=com.msg_load();
                    var rows=$("#dg").datagrid('getRows');
                    var params=$("#editForm").serialize();
                    var detail=JSON.stringify(rows);
                    params+="&detail="+detail;
                    $.ajax({
                        type: "POST",
                        url: "/dictionary/save",
                        data:params,
                        success: function (data) {
                            com.msg_load_close(index);
                            var d=eval(data);
                            if (data!=null&&d.msg=='success') {
                                $("#addView").dialog('close');
                                $("#tt").datagrid('reload');
                                com.msg_success(d.data);
                            } else {
                                $.messager.progress('close');
                                com.msg_error(d.msg);
                            }
                        }
                    });
                }
            }, {
                text: '关闭',
                iconCls: 'icon fa fa-times',
                handler: function () {
                    $("#addView").dialog('close');
                }
            }]
        });
//        $("#addView").dialog('maximize');
    });

    $("#edit").click(function () {
        var row=$("#tt").datagrid('getSelected');
        if (row==null){
            com.msg_error("请选择要操作的行");
            return false;
        }
        var id=row.id;
        $("<div></div>").dialog({
            id:'view',
            title:'编辑',
            resizable: true,
            closable: true,
            width: 600,
            height: 500,
            modal:true,
            href: "/dictionary/view?id="+id,
            onClose:function () {
                $("#view").dialog('destroy');
            },
            onDestroy: function () {
                $("#tt").datagrid('reload');
            },
            buttons:[{
                iconCls: 'icon fa fa-check',
                text: '保存',
                handler: function () {
                    var index=com.msg_load();
                    var rows=$("#dg").datagrid('getRows');
                    var params=$("#editForm").serialize();
                    var detail=JSON.stringify(rows);
                    params+="&detail="+detail;
                    $.ajax({
                        type: "POST",
                        url: "/dictionary/save",
                        data:params,
                        success: function (data) {
                            com.msg_load_close(index);
                            var d=eval(data);
                            if (data!=null&&d.msg=='success') {
                                $("#view").dialog('close');
                                $("#tt").datagrid('reload');
                                com.msg_success(d.data);
                            } else {
                                $.messager.progress('close');
                                com.msg_error(d.msg);
                            }
                        }
                    });
                }
            }, {
                text: '关闭',
                iconCls: 'icon fa fa-times',
                handler: function () {
                    $("#view").dialog('close');
                }
            }]
        });
//        $("#view").dialog('maximize');
    });

    $("#del").click(function () {
        var row=$("#tt").datagrid('getSelected');
        if (row==null){
            com.msg_error("请选择要操作的行");
            return false;
        };
        var id=row.id;
        com.msg_confirm("确认要删除吗？",function () {
            $.ajax({
                type: 'POST',
                url: "/dictionary/del",
                data: {id: id},
                success: function (data) {
                    var d = eval(data);
                    if (data != null && d.msg == 'success') {
                        $("#tt").datagrid('reload');
                        com.msg_success(d.data);
                    } else {
                        com.msg_error(d.msg);
                    }
                }
            })
        })
    });

});

function getCurrentMonthFirst() {
    var date = new Date();
    date.setDate(1);
    return date;
}
function myformatter(date) {
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    var d = date.getDate();
    return y + '-' + (m < 10 ? ('0' + m) : m) + '-' + (d < 10 ? ('0' + d) : d);
}
function find() {
    $('#tt').datagrid('load',{
//        startTime:$('#startTime').datebox('getValue'),
//        endTime:$('#endTime').datebox('getValue'),
        code:$('#code').val(),
        name:$('#name').val(),
    })
}
</script>
</@js>

